package com.nforce.linremote.scripting;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.content.res.AssetManager;
import android.os.Environment;

public class FileHelper {

    @SuppressWarnings("resource")
    public static InputStream getFileForReading(String path) {
	if (path == null) {
	    return null;
	}
	InputStream inputStream = null;
	if (isFromExternalStorage(path)) {
	    try {
		inputStream = new FileInputStream(path);
	    } catch (FileNotFoundException e) {
		e.printStackTrace();
	    }
	} else {
	    AssetManager assetManager = ContextHolder.getContext().getAssets();
	    try {
		inputStream = assetManager.open(path);
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	}
	return inputStream;
    }

    private static boolean isFromExternalStorage(String path) {
	if (path.startsWith(Environment.getExternalStorageDirectory().getAbsolutePath())) {
	    return true;
	}
	return false;
    }
}
